package com.ct.gd.passport.view.passport;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TableRow;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.domain.PassportGoApply;
import com.ct.gd.passport.service.PassportService;
import com.up91.common.android.async.ILoading;
import com.up91.common.android.async.SimpleAsyncTask;
import com.up91.common.android.helper.ToastHelper;

public class PassportHistoryDetailActivity extends BaseActivity implements OnClickListener{

	TextView mTvArea;
	TextView mTvPassId;
	TextView mTvPassDate;
	TextView mTvBirthday;
	TextView mTvName;
	TextView mTvPhotoNumber;
	
	TableRow mTrHongKong;
	TableRow mTrMacao;
	TextView mTvValidityHK;
	TextView mTvValidityMC;
	
	TableRow mTrSmsSjr;
	TableRow mTrSmsAddr;
	TableRow mTrSmsYb;
	TextView mTvNeedExpress;
	TextView mTvSmsSjr;
	TextView mTvSmsAddr;
	TextView mTvSmsYb;
	
	Button mQueryButton;
	Button mResubmitButton;
	
	ImageButton backButton;
	TextView headerTv;
	
	TextView mTvGoArea;
	
	PassportGoApply mCurrentApply;
	
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.passport_history_detail);
	}

	@Override
	protected void initViews() {
		backButton = (ImageButton)findViewById(R.id.btn_header_left);
		backButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PassportHistoryDetailActivity.this.finish();
			}
		});
		headerTv = (TextView)findViewById(R.id.tv_header_center);
		headerTv.setText("签注信息");
		
		mTvArea = (TextView)findViewById(R.id.grly_log_tv_area);
		mTvPassId = (TextView)findViewById(R.id.grly_log_tv_pass);
		mTvPassDate = (TextView)findViewById(R.id.grly_log_tv_passdate);
		mTvBirthday = (TextView)findViewById(R.id.grly_log_tv_birthday);
		mTvName = (TextView)findViewById(R.id.grly_log_tv_name);
		mTvPhotoNumber = (TextView)findViewById(R.id.grly_log_tv_phonenumber);
		mTvValidityHK = (TextView)findViewById(R.id.grly_log_tv_hongkong);
		mTvValidityMC = (TextView)findViewById(R.id.grly_log_tv_macao);
		mTvNeedExpress = (TextView)findViewById(R.id.grly_log_tv_needexpress);
		mTvSmsSjr = (TextView)findViewById(R.id.grly_log_tv_emssjr);
		mTvSmsAddr = (TextView)findViewById(R.id.grly_log_tv_emsaddr);
		mTvSmsYb = (TextView)findViewById(R.id.grly_log_tv_emsyb);
		
		mTrHongKong = (TableRow)findViewById(R.id.grly_log_tr_hk);
		mTrMacao = (TableRow)findViewById(R.id.grly_log_tr_mc);
		mTrSmsSjr = (TableRow)findViewById(R.id.grly_log_tr_emssjr);
		mTrSmsAddr = (TableRow)findViewById(R.id.grly_log_tr_emsaddr);
		mTrSmsYb = (TableRow)findViewById(R.id.grly_log_tr_emsyb);
		
		mTvGoArea = (TextView)findViewById(R.id.grly_log_tv_hktype);
		
		mQueryButton = (Button)findViewById(R.id.btn_passport_history_query);
		mResubmitButton = (Button)findViewById(R.id.btn_passport_history_submit);
		mQueryButton.setOnClickListener(this);
		mResubmitButton.setOnClickListener(this);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		String id = getIntent().getStringExtra("id");
		if(id!=null){
			new QueryPassportTask(this).execute(id);
		}
		
//		PassportGoApply apply = getIntent().getParcelableExtra("goapply");
//		setFormByApply(apply);
	}
	
	private void setFormByApply(PassportGoApply apply){
		if(apply != null){
			mTvPassId.setText(apply.getPassportId());
			mTvPassDate.setText(apply.getPassLimitDate());
			mTvBirthday.setText(apply.getBirthday());
			mTvName.setText(apply.getName());
			mTvPhotoNumber.setText(apply.getPhoneNumber());
			if(apply.getArea()!=null){
				mTvGoArea.setText(apply.getArea().name);
			}else{
				mTvGoArea.setText("未知");
			}
			mCurrentApply = apply;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_passport_history_query:
			if(mCurrentApply!=null){
				Intent  intent = new Intent();
				intent.putExtra("number", mCurrentApply.getSerial());
				intent.setClass(this, PassportQueryActivity.class);
				startActivity(intent);
			}else{
				ToastHelper.toast(this, "没有签注记录");
			}
			break;
			
		case R.id.btn_passport_history_submit:
			if(mCurrentApply!=null){
				Intent  intent = new Intent();
				intent.putExtra("goapply", mCurrentApply);
//				intent.putExtra("number", mCurrentApply.getSerial());
				intent.setClass(this, PassportApp2Activity.class);
				startActivity(intent);
				finish();
			}else{
				ToastHelper.toast(this, "没有签注记录");
			}
			break;
			
		default:
			break;
		}
		
	}
	
	private class QueryPassportTask extends SimpleAsyncTask<String, Void, PassportGoApply>{

		public QueryPassportTask(ILoading loading) {
			super(loading);
		}

		@Override
		protected PassportGoApply onLoad(String... params) throws Exception {
			String id = params[0];
			return PassportService.queryPassportById(id);
		}

		@Override
		protected void onUI(PassportGoApply result) {
			if(result!=null){
				setFormByApply(result);
//				Intent intent = new Intent(PassportHistoryDetailActivity.this, PassportHistoryDetailActivity.class);
////				intent.putExtra("id", info.getSerial());
//				intent.putExtra("goapply", result);
//				startActivity(intent);
			}else{
				ToastHelper.toast(getContext(), "无法查询到该表单记录，请重试!");
			}
		}
		
	}
}
